package controller;

import POJO.UserInfo;
import lombok.Data;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import services.UserInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author XW
 * @Date 2022/4/24
 * @Time 15:51
 **/
@Data
@ToString
@Controller
public class UserInfoController {
    @Autowired
    UserInfoService userInfoService;
    @RequestMapping("/selectAll")
    public String selectUserInfo(HttpServletResponse response, HttpServletRequest request) {
        List<UserInfo> userInfoList = userInfoService.selectAll();
        if (userInfoList != null) {
            request.setAttribute("users",userInfoList);
            return "userInfo.jsp";
        } else {
            return "index.jsp";
        }
    }
    @RequestMapping("/deleteUserInfo")
    public String deleteUserInfo(HttpServletResponse response, HttpServletRequest request) {
        int count = userInfoService.deleteUser(Integer.parseInt(request.getParameter("id")));
        if (count==0) {
            request.setAttribute("info","删除失败");
            return "deleteUser.jsp";
        } else {
            request.setAttribute("info","删除成功");
            return "deleteUser.jsp";
        }
    }
    @RequestMapping("/updateUserInfo")
    public String updateUserInfo(HttpServletResponse response, HttpServletRequest request) {
        UserInfo userInfo=new UserInfo();
        userInfo.setId(Integer.valueOf(request.getParameter("id")));
        userInfo.setUserName(request.getParameter("userName"));
        userInfo.setPassword(request.getParameter("password"));
        userInfo.setGender(request.getParameter("gender"));
        userInfo.setAddr(request.getParameter("addr"));
        System.out.println(userInfo);
        int count = userInfoService.updateUser(userInfo);
        if (count==0) {
            request.setAttribute("info","更新失败");
            return "updateUser.jsp";
        } else {
            request.setAttribute("info","更新成功");
            return "updateUser.jsp";
        }
    }
    @RequestMapping("/addUserInfo")
    public String insertUserInfo(HttpServletResponse response, HttpServletRequest request) {
        UserInfo userInfo=new UserInfo();
        userInfo.setUserName(request.getParameter("userName"));
        userInfo.setPassword(request.getParameter("password"));
        userInfo.setGender(request.getParameter("gender"));
        userInfo.setAddr(request.getParameter("addr"));
        System.out.println(userInfo);
        int count = userInfoService.addUser(userInfo);
        if (count==0) {
            request.setAttribute("info","添加失败");
            return "addUser.jsp";
        } else {
            request.setAttribute("info","添加成功");
            return "addUser.jsp";
        }
    }
    @RequestMapping("/login")
    public String login(HttpServletResponse response, HttpServletRequest request) {
        List<UserInfo> userInfos = userInfoService.login(request.getParameter("userName"), request.getParameter("password"));
        System.out.println(userInfos.size());
        if (userInfos.size()!=0 && userInfos!=null) {
            return "index.jsp";
        } else {
            return "error.jsp";
        }
    }
    @RequestMapping("/regist")
    public String regist(HttpServletResponse response, HttpServletRequest request) {
        UserInfo userInfo=new UserInfo();
        userInfo.setUserName(request.getParameter("userName"));
        userInfo.setPassword(request.getParameter("password"));
        userInfo.setGender(request.getParameter("gender"));
        userInfo.setAddr(request.getParameter("addr"));
        System.out.println(userInfo);
        int count = userInfoService.addUser(userInfo);
        if (count==0) {
            return "registError.jsp";
        } else {
            return "login.jsp";
        }
    }
}
